package it.xiuxian.lib.utils

import android.text.InputFilter
import android.text.Spanned
import android.widget.EditText
import java.util.regex.Matcher
import java.util.regex.Pattern

object EditTextFilter {
    /**
     * 禁止EditText输入空格
     *
     * @param editText
     */
    fun setEditTextInhibitInputSpaChat(editText: EditText) {
        val filter_space: InputFilter = object : InputFilter {
          override  fun filter(
              source: CharSequence,
              start: Int,
              end: Int,
              dest: Spanned?,
              dstart: Int,
              dend: Int
            ): CharSequence? {
                return if (source == " ") "" else null
            }
        }
        val filter_speChat: InputFilter = object : InputFilter {
           override fun filter(
                charSequence: CharSequence,
                i: Int,
                i1: Int,
                spanned: Spanned?,
                i2: Int,
                i3: Int
            ): CharSequence? {
                val speChat = "[`~!@#_$%^&()+=|{}':;',\\[\\].<>/?~！@#￥%……&（）— +|{}【】‘；：”“’。，、？]"
                val pattern: Pattern = Pattern.compile(speChat)
                val matcher: Matcher = pattern.matcher(charSequence.toString())
               if(matcher.find()){
                   ToastUtil.showShort("请不要输入特殊字符")
                   return ""
               }else
                return null
            }
        }
        editText.setFilters(arrayOf<InputFilter>(filter_space, filter_speChat))
    }

}